/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package k3tool.common.filter;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import nu.xom.Builder;
import nu.xom.DocType;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;
import nu.xom.XPathContext;
import org.apache.commons.io.FileUtils;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 *
 * @author Jeff Huang
 */
public abstract class XHtmlConstants {

    public static final String namespace = "http://www.w3.org/1999/xhtml";
    //public static final DocType docType =

    public static DocType createXHtmlDocType() {
        return new DocType("html", "-//W3C//DTD XHTML 1.1//EN",
                "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
    }

    public static Document createXHtmlDocument() {
        Element root = new Element("html", namespace);
        Document doc = new Document(root);
        doc.insertChild(createXHtmlDocType(), 0);
        return doc;
    }

    /**
     * @param localFile
     * @return
     */
    public static URL getBaseUrl(File localFile) {
        File baseUrlFile = new File(localFile.getAbsoluteFile().getParent(), "." + localFile.getName() + ".baseurl");
        if (!baseUrlFile.exists()) {
            return null;
        }
        try {
            String baseUrl = FileUtils.readFileToString(baseUrlFile);
            baseUrl = baseUrl.trim();
            if (baseUrl.isEmpty()) {
                return null;
            }
            return new URL(baseUrl);
        } catch (IOException ex) {
            return null;
        }
    }

    public static Builder getXHtmlDocBuilder() throws SAXException {
        XMLReader tagsoup = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
        return new Builder(tagsoup);
    }

    public static interface XHtmlProcessor {

        public Object process(Document doc, XPathContext context);
    }

    public static Object processXHtmlDocument(File file, String encoding, XHtmlProcessor processor) throws Exception {
        XPathContext context = new XPathContext("html", XHtmlConstants.namespace);
        XMLReader tagsoup = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
        Builder builder = new Builder(tagsoup);

        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            InputStreamReader isr = null;
            if (encoding != null) {
                isr = new InputStreamReader(fis, encoding);
            }else{
                isr = new InputStreamReader(fis);
            }
            Document xhtmlDoc = builder.build(isr);
            Object result = processor.process(xhtmlDoc, context);
            return result;
        }finally{
            if(fis != null){
                try{
                    fis.close();
                }catch(IOException ex){
                    //do nothing
                }
            }
        }



    }
}
